package com.bdcom.autotest.ui.snp;

import com.bdcom.autotest.interaction.bean.DeviceUpdBean;
import com.bdcom.autotest.interaction.http.ConsoleServerOper;
import com.bdcom.autotest.interaction.http.OperDevice;
import com.bdcom.autotest.interaction.http.callback.EnterMonitorCallBack;
import com.bdcom.autotest.interaction.step.StepThread;
import com.bdcom.autotest.interaction.step.remodel.UpgradeDev;
import com.bdcom.autotest.util.Prop;
import com.bdcom.autotest.util.UIUtil;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Map;

public class PanelForInputDevSN extends AbsPanel {
	private boolean flag  = false;
	private boolean result  = false;
    private static Logger log = LoggerFactory.getLogger(PanelForInputDevSN.class);
	public PanelForInputDevSN(StepThread mainrun,int idx) {
		this.setBackground(Color.WHITE);
		this.setPreferredSize(new Dimension( 0 , 250 ));
		JPanel p1 = UIUtil.createPanel();
		Font font2 = new Font("Dialog",Font.PLAIN, 14);
		JLabel jLabel0 = new JLabel( "设备"+idx+" Mac:");
		jLabel0.setFont(font2);
		JTextField wsn = new JTextField(18);
		wsn.setName("0");
		wsn.setFont(font2);
		wsn.setText("");
		wsn.addKeyListener((new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				super.keyTyped(e);
				if(e.getKeyChar() == KeyEvent.VK_ENTER){
					try{
						String[] sns = mainrun.getMf().getLeftp().getsns();
						String mac = wsn.getText().trim();
						sns[idx-1] = mac;
						mainrun.setSNs(sns);
						if(!"".equals(mac)){
							Map<String, DeviceUpdBean> mcaToUpdBean =mainrun.getMacToUpdBean();
							DeviceUpdBean updBean = mcaToUpdBean.get(mac);
							if(updBean != null){
								flag = true;
								result =true;
								updBean.setConsoleSeverPort(Prop.console_server_port[idx-1]);
								//扫完format，重启，修改SN MAC
								EnterMonitorCallBack enterMonitorCallBack = new EnterMonitorCallBack(mainrun);
								OperDevice.enterMonitor(updBean, enterMonitorCallBack);
							}else{
								JOptionPane.showMessageDialog(null, "在工单中不存在！");
								return;
							}
						}else{
							flag = true;
							result =true;
						}
					}catch (Exception exception){
						log.error("",exception);
					}

				}
			}
		}));
		p1.add(jLabel0);
		p1.add(wsn);
		p1.add(Box.createHorizontalStrut(10) );
		JPanel p2 = UIUtil.createPanel();
		JButton btn2 = new JButton("确认");
		btn2.addActionListener( new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			    try{
                    String[] sns = mainrun.getMf().getLeftp().getsns();
                    String mac = wsn.getText().trim();
//				if("".equals(oemSN)){
//					flag = true;
//					result =false;
//					mainrun.addErrorLog("输入的SN不正确");
//				}else{
                    sns[idx-1] = mac;
                    mainrun.setSNs(sns);
//				}
                    if(!"".equals(mac)){
                        Map<String, DeviceUpdBean> mcaToUpdBean =mainrun.getMacToUpdBean();
                        DeviceUpdBean updBean = mcaToUpdBean.get(mac);
                        if(updBean != null){
                            flag = true;
                            result =true;
                            updBean.setConsoleSeverPort(Prop.console_server_port[idx-1]);
                            //扫完format，重启，修改SN MAC
                            EnterMonitorCallBack enterMonitorCallBack = new EnterMonitorCallBack(mainrun);
                            OperDevice.enterMonitor(updBean, enterMonitorCallBack);
                        }else{
                            JOptionPane.showMessageDialog(null, "在工单中不存在！");
                            return;
                        }
                    }else{
                        flag = true;
                        result =true;
                    }
                }catch (Exception exception){
			        log.error("",exception);
                }

			}
		});
		p2.add(Box.createHorizontalStrut( 20 ));
		p2.add(btn2);
		this.add(p1);
		this.add(p2);
	}

	public boolean getTotalResult(){
		while(!flag){
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return result;
	}
}
